package com.leon.learnenginespace.drools.utils.operation.impl;

import com.leon.learnenginespace.drools.utils.MathUtils;
import com.leon.learnenginespace.drools.utils.operation.IOperationBase;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 小于
 * @author DJ032474
 */
@Component
public class LessOperation implements IOperationBase {
    public static void main(String[] args) {
        LessOperation greaterEqualsModel = new LessOperation();
        Date date = new Date();
    //    date.setTime(System.currentTimeMillis() +10000);
        Date date2 = new Date();
     //   date2.setTime(date.getTime()+10);
        System.out.println(greaterEqualsModel.operation( "date",date2,date));
    }
    /**
     * 日期比较
     * @param v1
     * @param v2
     * @return
     */
    private Boolean dateCompareTo(Object v1, Object v2) {
        Date v1Date= (Date)v1;
        Date v2Date = (Date)v2;
        int result = 0;
        if (v1 != null) {
            result = v1Date.compareTo(v2Date);
            return isBoolean(result);
        }

        if (v2 != null) {
            result = v2Date.compareTo(v1Date);
            return isBoolean(result * -1);
        }
        
        return true;
    }

    /**
     * 数值比较
     * @param v1
     * @param v2
     * @return
     */
    private boolean numberCompareTo(Object v1, Object v2){
        BigDecimal v1BigDecimal = MathUtils.getBigDecimal(v1);
        BigDecimal v2BigDecimal = MathUtils.getBigDecimal(v2);
        int result = 0;
        if (v1 != null) {
            result = v1BigDecimal.compareTo(v2BigDecimal);
            return isBoolean(result);
        }
        if (v2 != null) {
            result = v2BigDecimal.compareTo(v1BigDecimal);
            return isBoolean(result);
        }
        return true;
    }
    /**
     * 字符串比较
     * @param v1
     * @param v2
     * @return
     */
    private boolean strCompareTo(Object v1, Object v2){
        String v1Str = String.valueOf(v1);
        String v2Str = String.valueOf(v2);
        int result = 0;
        if (v1 != null) {

            result = v1Str.compareTo(v2Str);
            return isBoolean(result);
        }
        if (v2 != null) {
            result = v2Str.compareTo(v1Str);
            return isBoolean(result * -1);
        }
        return  true;
    }

    /**
     * 比较结果转换boolean
     * @param result
     * @return
     */
    private Boolean isBoolean(int result){
        if(result == 0){
            return false;
        } else if(result > 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * v1<v2 只支持字符串，数值
     * @param v 值
     * @return
     */
    @Override
    public Boolean operation(String dataType, Object ...v) {
        Object v1 = v[0];
        Object v2 = v[1];
        try {
           if(ObjectUtils.nullSafeEquals(v1, v2)) {
                return false;
           }
           if (v1 instanceof String || v2 instanceof String) {
               return strCompareTo(v1, v2);
           } else if(v1 instanceof Date && v2 instanceof Date){
               return dateCompareTo(v1, v2);
           } else {
               return numberCompareTo(v1, v2);
           }
       }catch (Exception e){
           return false;
       }
    }
}
